package com.itheima.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;

import com.itheima.common.TokenHolder;
import com.itheima.entity.dto.category.CategoryDto;
import com.itheima.entity.dto.category.CategoryUpdateDto;
import com.itheima.entity.pojo.Category;
import com.itheima.entity.vo.category.CategoryAllPageVo;
import com.itheima.entity.vo.category.CategoryAllVo;
import com.itheima.entity.vo.category.CategoryListVo;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-04
 */
@Service

public class CategoryServiceImpl implements CategoryService {

    @Autowired
    SetmealMapper setmealMapper;



    @Autowired
    CategoryMapper categoryMapper;

    /**
     * -新增分类
     *
     * @param categoryDto
     * @return
     */
    @Override
    public Boolean addCategory(CategoryDto categoryDto) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        //1.判断是否存在相应的数据
        Integer integer = categoryMapper.selectByCategory(category);

        if(integer>0){
            return false;
        }

        //获取id
        Snowflake snowflake = new Snowflake();
        String idStr = snowflake.nextIdStr();
        category.setId(idStr);

        //获取当前时间
        Date date = DateUtil.date();
        category.setCreateTime(date);
        category.setUpdateTime(date);

        //获取当前用户
        String Id = TokenHolder.getCurrentId();

        category.setCreateUser(Id);
        category.setUpdateUser(Id);

        return   categoryMapper.insert(category)>0;
    }

    /**
     * 更新分类
     *
     * @param categoryUpdateDto
     */
    @Override
    public Boolean updateCategory(CategoryUpdateDto categoryUpdateDto) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryUpdateDto, category);
        category.setUpdateTime(new Date());
        category.setSort(categoryUpdateDto.getSort());

        return categoryMapper.update(category)>0;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteCategory(String id) {

        Integer count =setmealMapper.selectByCategoryId(id);
        if(count>0){
            return false;
        }

        return categoryMapper.deleteById(id)>0;
    }

    /**
     * 获取所有
     *
     * @return
     */
    @Override
    public List<CategoryAllVo> getCategoryAll(Integer type) {
        List<Category> categories = categoryMapper.selectAll(type);
        List<CategoryAllVo> list =new ArrayList<>();
        if (CollUtil.isEmpty(categories)) {
            return null;
        }

        for (Category category : categories) {
            if (Objects.isNull(category)) {
                continue;
            }
            CategoryAllVo categoryAllVo = new CategoryAllVo();
            BeanUtils.copyProperties(category, categoryAllVo);
            list.add(categoryAllVo);
        }


        return list;
    }

    /**
     * 根据分页获取所有
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public CategoryAllPageVo getCategoryAllPage(Integer page, Integer pageSize) {


        CategoryAllPageVo categoryAllPageVo = new CategoryAllPageVo();
        categoryAllPageVo.setCurrent(page);
        categoryAllPageVo.setSize(pageSize);
        //获取总条数
        Integer count =categoryMapper.selectCount();
        categoryAllPageVo.setTotal(count);


        page=(page-1)*pageSize;
        List<Category> categories = categoryMapper.selectAllPage(page,pageSize);

        categoryAllPageVo.setRecords(categories);


        return categoryAllPageVo;
    }

    /**
     * 获取分类类别
     *
     * @return
     */
    @Override
    public List<CategoryListVo> getCategoryList() {
        List<CategoryListVo> categoryListVos = new ArrayList<>();



        //1获取所有分类
        List<Category>  categories =categoryMapper.selectList();

        //2.判断是否为空
        if(CollUtil.isEmpty(categories)){
            return categoryListVos;
        }
        //3.转换成前端对象
        for (Category category : categories) {
            if(Objects.isNull(category)){
                continue;
            }
            CategoryListVo categoryListVo = new CategoryListVo();

            BeanUtils.copyProperties(category, categoryListVo);

            categoryListVos.add(categoryListVo);

        }




        return categoryListVos;
    }


}
